<?php namespace App\Utils\Tree;


class TreeNode
{

    var $id;
    var $name;
    var $parentId;
    var $parentNode;
    var $icon;
    var $showChapterNo = false;
    var $showCheckBox = false;
    var $clickMethod = "";
    var $children = array();
    var $lines = array();
    var $checked = false;
    var $rank = 0;
    var $sequence = 0;
    var $subSequence = 0;
    var $chapterNo = "";
    var $shadowIndex;
    var $display = 'Y';
    var $open = 'Y';
    var $openFolderIcon = "../../p5/tree/folderOpen.png";
    var $closeFolderIcon = "../../p5/tree/folderOpen.png";
    var $attributes = array();

    public function __construct($id, $name, $parentId = null, $icon = "")
    {
        $this->id = $id;
        $this->name = $name;
        $this->parentId = $parentId;
        $this->icon = $icon;
        $this->open = "Y";
        $this->display = "Y";
    }

    public function refreshChapterNo()
    {
        if ($this->parentNode == null) {
            $this->chapterNo = "";
        } else {
            $temp = $this->parentNode->chapterNo;
            if ($temp == '') {
                $this->chapterNo = $this->subSequence + 1;
            } else {
                $this->chapterNo = $temp . "." . ($this->subSequence + 1);
            }
        }
    }

    public function refreshRank()
    {
        if ($this->parentNode == null || $this->parentId == "") {
            $this->rank = 0;
        } else {
            $this->rank = ($this->parentNode->rank + 1);
        }
    }

    public function findAncestorById($ancestorId)
    {
        if ($this->parentNode == null || $this->parentId == "") {
            return null;
        }
        $node = $this->parentNode;
        if ($node->id == $ancestorId) {
            return $node;
        } else {
            return $node->findAncestorById($ancestorId);
        }
    }

    public function hasChild()
    {
        return count($this->children) > 0;
    }

    public function hasElderBrother()
    {
        return $this->subSequence > 0;
    }

    public function hasYoungerBrother()
    {
        if ($this->parentNode == null || $this->parentId == "") {
            return false;
        }
        $c = count($this->parentNode->children) - 1;
        return $c > $this->subSequence;
    }

    public function getChildren()
    {
        return $this->children;
    }

    public function addChild($node)
    {
        $node->subSequence = count($this->children);
        $node->parentId = $this->id;
        $node->parentNode = $this;
        $node->rank = $this->rank + 1;
        array_push($this->children, $node);
    }

    public function removeChild($id)
    {
        $matrix = array();
        $size = $this->childrenSize();
        $begin = false;
        $child = "";
        for ($i = 0; $i < $size; $i++) {
            $node = $this->children[$i];
            if ($node->id == $id) {
                $child = $node;
                $begin = true;
                continue;
            }
            if ($begin) {
                $node->subSequence--;
            }
            array_push($matrix, $node);
        }
        $this->children = $matrix;
        unset($child);
    }

    public function childrenSize()
    {
        return count($this->children);
    }

    public function nextSibling()
    {
        if ($this->hasYoungerBrother()) {
            return $this->parentNode->children[$this->subSequence + 1];
        }
        return null;
    }

    public function getDebugName()
    {
        return "id=" . $this->id . ",name=" . $this->name . ",parentId=" . $this->parentId . ",rank=" . $this->rank . ",sequence=" . $this->sequence . ",subSequence=" . $this->subSequence . ",display=" . $this->display . ",open=" . $this->open;
    }

    public function getName()
    {
        if ($this->showChapterNo) {
            return $this->chapterNo . ". " . $this->name;
        }
        return $this->name;
    }

    public function changeOpenStatus()
    {
        if ($this->open == "Y") {
            $this->open = "N";
        } else {
            $this->open = "Y";
        }
    }

    public function getIcon()
    {
        if ($this->icon != "") {
            return $this->icon;
        }
        if ($this->isOpen()) {
            return $this->openFolderIcon;
        }
        return $this->closeFolderIcon;
    }

    public function isOpen()
    {
        return $this->open == "Y";
    }

    public function isDisplay()
    {
        return $this->display == "Y";
    }

    public function setAttribute($name, $value)
    {
        $this->attributes[$name] = $value;
    }

    public function getAttribute($name, $defaultValue = "")
    {
        if (array_key_exists($name, $this->attributes)) {
            return $this->attributes[$name];
        }
        return $defaultValue;
    }

    public function removeAttribute($name, $defaultValue = "")
    {
        if (array_key_exists($name, $this->attributes)) {
            unset($this->attributes[$name]);
        }
    }

    public function refreshDisplay()
    {

    }

    public function toArray()
    {
        $me = ["id" => $this->id, "name" => $this->name, "parentId" => $this->parentId];
        $me = array_merge($me, $this->attributes);
        $children = [];
        foreach ($this->children as $child) {
            array_push($children, $child->toArray());
        }
        $me["children"] = $children;
        return $me;
    }
}